package co.fitstart.scheduler.job;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.scheduler.service.SmsNoticeService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Component
public class SmsNoticeJob {
    
    private static final Logger logger = LoggerFactory.getLogger(SmsNoticeJob.class);
    
    @Autowired
    private SmsNoticeService smsNoticeService;
    
    @Scheduled(cron = "0 45 9 * * *")
    public void twoWeeksLaterExpireNotice() {
        Date now = new Date();
        
        logger.info("start running SmsNoticeJob#twoWeeksLaterExpireNotice at {}", now);
        
        // scan order pay timeout
        int toExpireRound = smsNoticeService.toExpiryCourseRound(now);
        logger.info("scaned {} courseRound will expire two weeks later", toExpireRound);
        
        if(toExpireRound > 0) {
            smsNoticeService.noticeToExpireRound(now);
        }
        
        logger.info("finish running SmsNoticeJob#scanToExpireRound at {}", new Date());
    }

    /**
     * 提前两小时上课提醒（古北店、五角场店、复兴店除外）
     */
    @Scheduled(cron = "0 0/30 7-22 * * *")
    public void twoHoursLaterScheduleNotice() {
        Date now = new Date();
        
        logger.info("start running SmsNoticeJob#twoHoursLaterScheduleNotice at {}", now);
        
        // scan order pay timeout
        int toExpireRound = smsNoticeService.toNoticeSchedule(now);
        logger.info("scaned {} courseSchedule will occur two hours later", toExpireRound);
        
        if(toExpireRound > 0) {
            smsNoticeService.noticeSchedule(now);
        }
        
        logger.info("finish running SmsNoticeJob#twoHoursLaterScheduleNotice at {}", new Date());
    }
    
    @Scheduled(cron = "0 45 8 * * *")
    public void tenDaysLaterUnFreezeNotice() {
        Date now = new Date();
        
        logger.info("start running SmsNoticeJob#tenDaysLaterUnFreezeNotice at {}", now);
        
        // scan order pay timeout
        int toExpireRound = smsNoticeService.toUnfreezeCourseRound(now);
        logger.info("scaned {} courseSchedule will occur two hours later", toExpireRound);
        
        if(toExpireRound > 0) {
            smsNoticeService.noticeToUnfreezeRound(now);
        }
        
        logger.info("finish running SmsNoticeJob#tenDaysLaterUnFreezeNotice at {}", new Date());
    }
    
    /**
     * 仅用于古北店和五角场店
     */
    @Scheduled(cron = "0 15 8 * * *")
    public void todayScheduleNotice() {
        Date now = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
        
        logger.info("start running SmsNoticeJob#todayScheduleNotice at {}", now);
        
        int scheduleAmount = smsNoticeService.toNoticeScheduleToday(now);
        logger.info("scaned {} courseSchedule will occur today", scheduleAmount);
        
        if(scheduleAmount > 0) {
            smsNoticeService.noticeScheduleToday(now);
        }
        
        logger.info("finish running SmsNoticeJob#todayScheduleNotice at {}", new Date());
    }
    
    /**
     * 仅用于复兴店
     */
    @Scheduled(cron = "0 5 9 * * *")
    public void todayScheduleNoticeAtNine() {
        Date now = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
        
        logger.info("start running SmsNoticeJob#todayScheduleNoticeAtNine at {}", now);
        
        int scheduleAmount = smsNoticeService.toNoticeScheduleTodayAtNine(now);
        logger.info("scaned {} courseSchedule will occur today", scheduleAmount);
        
        if(scheduleAmount > 0) {
            smsNoticeService.noticeScheduleTodayAtNine(now);
        }
        
        logger.info("finish running SmsNoticeJob#todayScheduleNoticeAtNine at {}", new Date());
    }
}